import {AfterViewInit, Component, OnInit} from '@angular/core';
import {BadgePipe, MessageService, PaginationService, Table} from '../../../../../ng-tools';
import {ChargeRecordFormatterService} from '../../../../services/formatter/charge-record-formatter.service';
import {TicketService} from '../../../../services/api/ticket.service';
import {HttpParams} from '@angular/common/http';
import {Router} from '@angular/router';

@Component({
  selector: 'charge-record',
  templateUrl: './charge-record.component.html',
  styleUrls: ['./charge-record.component.scss'],
  providers: [BadgePipe]
})
export class ChargeRecordComponent implements OnInit, AfterViewInit {
    breadcrumb = [
        {name: '充值记录', cssIcon: 'fa fa-fw fa-list'}
    ];
    page = 1;
    pagenation = new PaginationService(null, null);
    chargeTable = new Table({
        headers: this.chargeFormatter.doneFormatter,
    });

    constructor(private message: MessageService,
                private ticketService: TicketService,
                private router: Router,
                private chargeFormatter: ChargeRecordFormatterService,
                ) {
    }

    ngOnInit() {
        this.getInfo(1);

    }
  getInfo(page) {
    this.pagenation.simpleFormatter = this.chargeTable.options.headers;
    this.ticketService.get(this.ticketService.urls.getOrderList, {
      observe: 'response',
      params: new HttpParams()
        .append('web_token', localStorage.getItem('token'))
        .append('orderType', '1')
        .append('pageNum', page + '')
        .append('pageSize', '10')
        .append('status', '1,2')}).subscribe((resp) => {
          if (resp.body.success) {
            const result = resp.body.returnValue.page;
            this.pagenation.page.results = result.list;
            this.pagenation.page.totalPage = result.pages;
            this.pagenation.page.pageNumber = result.pageNum;
            this.pagenation.formatData();
            this.message.sendMessage('page load progress', this);
          } else {
            this.router.navigateByUrl('/login').catch();
          }
    });
  }
  firstPage() {
        this.page = 1;
    this.getInfo(1);
  }
  previousPage() {
    if (this.page > 1) {
      this.page = this.page - 1;
    } else {
      this.page = 1;
    }
    this.getInfo(this.page);
  }
  nextPage() {
    if (this.page < this.pagenation.page.totalPage) {
      this.page = this.page + 1;
    } else {
      this.page = this.pagenation.page.totalPage;
    }
    this.getInfo(this.page);
  }
  lastPage() {
        this.page = this.pagenation.page.totalPage;
        this.getInfo(this.pagenation.page.totalPage);
  }
    ngAfterViewInit(): void {
        this.message.sendMessage('breadcrumb changed', this, this.breadcrumb);
        this.message.sendMessage('page load progress', this, '300ms');
    }
}
